//
//  HomeTableViewController.swift
//  Weibo007
//
//  Created by apple on 15/6/23.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit

class HomeTableViewController: BaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        visitor?.setupViewInfo("visitordiscover_feed_image_house", messages: "关注一些人，回这里看看有什么惊喜", isHome: true)
        setupNavigationBar()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 开启动画
        visitor?.startAnimation()
    }
    private func setupNavigationBar()
    {
        let left = UIButton()
        left.frame = CGRectMake(0, 0, 30, 30)
        left.setImage(UIImage(named: "navigationbar_friendsearch"), forState: UIControlState.Normal)
        left.setImage(UIImage(named: "navigationbar_friendsearch_highlighted"), forState: UIControlState.Highlighted)
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: left)
        let right = UIButton()
        right.frame = CGRectMake(0, 0, 30, 30)
        right.setImage(UIImage(named: "navigationbar_pop"), forState: UIControlState.Normal)
        right.setImage(UIImage(named: "navigationbar_pop_highlighted"), forState: UIControlState.Highlighted)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: right)
        title = shareAccount!.name!
        
        let name = shareAccount!.name!
        let titleBtn = titleBUtton()
        titleBtn.sizeToFit()
        titleBtn.setTitle(name + " ", forState: UIControlState.Normal)
        titleBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        titleBtn.titleLabel?.font = UIFont.systemFontOfSize(17.0)
        titleBtn.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        titleBtn.addTarget(self, action: "titleButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        navigationItem.titleView = titleBtn
    }
    func titleButtonClicked() {
        let sb = UIStoryboard(name: "popover", bundle: nil)
        let vc = sb.instantiateViewControllerWithIdentifier("popoverSB")
        vc.transitioningDelegate = self
        vc.modalPresentationStyle  = UIModalPresentationStyle.Custom
        presentViewController(vc, animated: true, completion: nil)
    }
    var isPresented = false
}
extension HomeTableViewController :UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning
{
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        return popoverUIPresentationController(presentedViewController: presented, presentingViewController: presenting)
    }
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        return self
    }
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        //toVc = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        if isPresented
        {
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
            transitionContext.containerView()?.addSubview(toView!)
            toView?.transform = CGAffineTransformMakeScale(1.0, 0.0)
            toView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0.0)
            UIView .animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
                toView?.transform = CGAffineTransformIdentity
                }, completion: { (_) -> Void in
                    transitionContext.completeTransition(true)
            })
        }
        else
        {
            let fromView = transitionContext.viewForKey(UITransitionContextToViewKey)
            fromView?.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
    }
}